﻿using MySql.Data.MySqlClient;
using Org.BouncyCastle.Asn1.IsisMtt;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 南京数化易达测长测重
{
    public class m2_procedureprogres_volServer
    {
        private string connectionString = "Server=123.56.252.149;Database=vol_pro_njshyd;User ID=root;Password=andy123456;";

        public void Insert(m2_procedureprogres_vol item)
        {
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                string query = "INSERT INTO m2_procedureprogres_vol (codeBatch, Weight_auto, WidthCloth_auto, diameter_auto, mode) VALUES (@codeBatch, @Weight_auto, @WidthCloth_auto, @diameter_auto, @mode)";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@codeBatch", item.CodeBatch);
                cmd.Parameters.AddWithValue("@Weight_auto", item.WeightAuto);
                cmd.Parameters.AddWithValue("@WidthCloth_auto", item.WidthClothAuto);
                cmd.Parameters.AddWithValue("@diameter_auto", item.DiameterAuto);
                cmd.Parameters.AddWithValue("@mode", item.Mode);
                cmd.ExecuteNonQuery();
            }
        }

        public List<m2_procedureprogres_vol> GetAll(int Mode1,int Mode2)
        {
            var items = new List<m2_procedureprogres_vol>();
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                string query = "SELECT codeBatch, Weight_auto, WidthCloth_auto, diameter_auto, mode FROM m2_procedureprogres_vol where mode = @Mode1 or mode = @Mode2 ";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@Mode1", Mode1);
               cmd.Parameters.AddWithValue("@Mode2", Mode2);
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var item = new m2_procedureprogres_vol
                    {
                        CodeBatch = reader["codeBatch"] as string,
                        WeightAuto = reader["WeightAuto"] as double?,
                        WidthClothAuto = reader["WidthCloth_auto"] as double?,
                        DiameterAuto = reader["diameter_auto"] as double?,
                        Mode = reader["mode"] as int?
                    };
                    items.Add(item);
                }
            }
            return items;
        }
        public m2_procedureprogres_vol GetSingle(int Mode1, int Mode2,string codeBatch)
        {
            m2_procedureprogres_vol item = null; // 使用 null 初始化
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                string query = "SELECT codeBatch, Weight_auto, WidthCloth_auto, diameter_auto, mode,reason FROM m2_procedureprogres_vol  WHERE (mode = @Mode1 OR mode = @Mode2) AND codeBatch = @codeBatch LIMIT 1";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@Mode1", Mode1);
                cmd.Parameters.AddWithValue("@Mode2", Mode2);
                cmd.Parameters.AddWithValue("@codeBatch", codeBatch);
               
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read()) // 使用 if 来检查是否有数据
                    {
                        item = new m2_procedureprogres_vol
                        {
                            CodeBatch = reader["codeBatch"] as string,
                            WeightAuto = reader["Weight_auto"] as double?,
                            WidthClothAuto = reader["WidthClothAuto"] as double?,
                            DiameterAuto = reader["DiameterAuto"] as double?,
                            Mode = reader["Mode"] as int?,
                            reason = reader["reason"] as string
                        };
                    }
                }
            }
            return item; // 返回单个对象
        }

        //public void Update(m2_procedureprogres_vol item)
        //{
        //    using (MySqlConnection conn = new MySqlConnection(connectionString))
        //    {
        //        conn.Open();
        //        string query = "UPDATE m2_procedureprogres_vol SET Weight_auto = @Weight_auto, WidthCloth_auto = @WidthCloth_auto, diameter_auto = @diameter_auto, mode = @mode WHERE codeBatch = @codeBatch";
        //        MySqlCommand cmd = new MySqlCommand(query, conn);
        //        cmd.Parameters.AddWithValue("@codeBatch", item.CodeBatch);
        //        cmd.Parameters.AddWithValue("@Weight_auto", item.WeightAuto);
        //        cmd.Parameters.AddWithValue("@WidthCloth_auto", item.WidthClothAuto);
        //        cmd.Parameters.AddWithValue("@diameter_auto", item.DiameterAuto);
        //        cmd.Parameters.AddWithValue("@mode", item.Mode);
        //        cmd.ExecuteNonQuery();
        //    }
        //}


        public int Update(m2_procedureprogres_vol item)
        {
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                string query = "UPDATE m2_procedureprogres_vol SET Weight_auto = @Weight_auto, WidthCloth_auto = @WidthCloth_auto, diameter_auto = @diameter_auto, mode = @mode WHERE codeBatch = @codeBatch";
                using (MySqlCommand cmd = new MySqlCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("@codeBatch", item.CodeBatch);
                    cmd.Parameters.AddWithValue("@Weight_auto", item.WeightAuto);
                    cmd.Parameters.AddWithValue("@WidthCloth_auto", item.WidthClothAuto);
                    cmd.Parameters.AddWithValue("@diameter_auto", item.DiameterAuto);
                    cmd.Parameters.AddWithValue("@mode", item.Mode);

                    // 执行更新并返回受影响的行数
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public void UpdateMode(int Mode, string CodeBatch)
        {
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                string query = "UPDATE m2_procedureprogres_vol SET  mode = @mode WHERE codeBatch = @codeBatch";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@codeBatch", CodeBatch);
                cmd.Parameters.AddWithValue("@mode", Mode);
                cmd.ExecuteNonQuery();
            }
        }

        public void Delete(int codeBatch)
        {
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                string query = "DELETE FROM m2_procedureprogres_vol WHERE codeBatch = @codeBatch";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@codeBatch", codeBatch);
                cmd.ExecuteNonQuery();
            }
        }
    }
}
